package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

import maquinaP.Display;

import java.util.*;


class PanelPila extends JPanel {
	private static final long serialVersionUID = 1L;
	private Vector<Double> pila = null;  //  @jve:decl-index=0:
	private int C;
	private int H;
	private int alto = 150;
	private Display B;
	//private static int NUMVACIOS = 10;
	
	public PanelPila() {
		super();
		// No queremos que se pinte todo
		setOpaque( false ); 
		setLayout( new BorderLayout() );
		
		redimensionar();
    }
	
	public void setPila(Vector<Double> p, int C, int H, Display B){
		pila = p;
		this.C = C;
		this.H = H;
		this.B = B;
	}
	
	private void redimensionar() {
		this.setSize(150, alto);
		this.setMinimumSize(new Dimension(150, alto));
		this.setPreferredSize(new Dimension(150, alto));
	}
	
	/*protected void paintComponent( Graphics g ) {
		if (pila != null) {
			pintar(g);
			//repaint();
		}
		repaint();
    }*/
	
	protected void paintComponent( Graphics g ) {
		g.setColor( Color.black );
		g.setFont (new Font ("Arial", Font.BOLD, 12));
		g.drawString("Pila", 10 , 15 );
		pintar(g);
	}
	
	public void apila(Graphics g, int nelem){
		int x;
		x = (alto-5)-(18*(nelem+1));
		g.setFont (new Font ("Arial", Font.BOLD, 12));
		//pintar H
		if (nelem == H) {
			g.setColor(Color.BLUE );
			g.fillRect(105, x , 20, 15);
			g.setColor( Color.WHITE );
			g.drawString( "H", 110 , x+12 );
			g.setColor( Color.BLACK );
			g.drawLine(70, x+7, 105, x+7);
		}
		//pintar C
		if (nelem == C) {
			g.setColor(Color.MAGENTA );
			g.fillRect(105, x , 20, 15);
			g.setColor( Color.WHITE );
			g.drawString( "C", 110 , x+12 );
			g.setColor( Color.BLACK );
			g.drawLine(70, x+7, 105, x+7);
		}
		/*g.setColor(Color.red );
		g.fillRect(10, x , 60, 15);
		g.setColor( Color.black );*/
		
		//pintar elementos
		/*String s = "";
		//if (nelem != pila.size()){
			Double d = pila.get(nelem);
			if (d != Integer.MIN_VALUE){
				s+= d + "";
				vacios = 0;
			} else {
				vacios++;
			}
			//if (vacios < NUMVACIOS){
				g.setColor(Color.red );
				g.fillRect(10, x , 60, 15);
				g.setColor( Color.black );
				g.drawString( s, 30 , x+12 );
				g.drawString( nelem+"", 75 , x+12 );
				pintados++;
			//}
		/*} else {
			g.setColor(Color.red );
			g.fillRect(10, x , 60, 15);
			g.setColor( Color.black );
			g.drawString( s, 30 , x+12 );
			g.drawString( nelem+"", 75 , x+12 );
		}*/
		String s = "";
		if (nelem < pila.size()){
			Double d = pila.get(nelem);
			if (d != null){
			if (d != Integer.MIN_VALUE)
				s+= d + "";}
		}
		g.setColor(Color.red );
		g.fillRect(10, x , 60, 15);
		g.setColor( Color.black );
		g.drawString( s, 30 , x+12 );
		g.drawString( nelem+"", 75 , x+12 );
		
	}
	
	public void pintaDisplay(Graphics g){
		if (B != null){
			int niveles = B.numNiveles();
			String nivel = "";
			int x = (alto-5);
			g.setColor(Color.BLACK );
			g.drawLine(70, x-5, 130, x-5);
			for (int i = 0; i < niveles; i++){
				x = (alto-5)-(i+1)*18;
				g.setColor(Color.DARK_GRAY );
				g.fillRect(130, x , 65, 15);
				g.setColor(Color.WHITE );
				if ((i == 0) || B.getNivel(i) != 0){
					nivel = B.getNivel(i)+"";
				} else {
					nivel = "-";
				}
				g.drawString( "Nivel "+i+":  "+nivel, 135 , x+12 );
			}
		}
	}
	
	public void pintar(Graphics g){
		int nelems = 0;
		if (pila != null)
			nelems = pila.size()+6;
		alto = (nelems+2)*18;
		if (alto < 150)
			alto = 150;
		redimensionar();
		for (int i = 0; i < nelems; i++){
			apila(g,i);
		}
		pintaDisplay(g);
	}
}